package com.cai.listener.stateMachine;

import com.cai.enumerator.OrderStatusEnum;
import com.cai.pojo.BO.Order;
import lombok.extern.slf4j.Slf4j;
import org.springframework.messaging.Message;
import org.springframework.statemachine.annotation.OnTransition;
import org.springframework.statemachine.annotation.WithStateMachine;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Author czq
 * @Package com.cai.listener.stateMachine
 * @Date 2024/5/20 13:04
 * @description: 订单状态机监听
 */
@Component
@Transactional
@WithStateMachine //参与状态机事件的候选注解
@Slf4j
public class OrderStatusListener {

    @OnTransition(source = "WAIT_PAYMENT",target = "WAIT_DELIVER")
    public boolean payTransition(Message message) {
        Order order = (Order)message.getHeaders().get("order");
        assert order != null;
        order.setOrderStatus(OrderStatusEnum.WAIT_DELIVER);
        log.info("支付，状态机反馈信息：{}",message.getHeaders());
        return true;
    }

    @OnTransition(source = "WAIT_DELIVER",target = "WAIT_RECEIVE")
    public boolean deliverTransition(Message message) {
        Order order = (Order)message.getHeaders().get("order");
        assert order != null;
        order.setOrderStatus(OrderStatusEnum.WAIT_RECEIVED);
        log.info("发货，状态机反馈信息：{}",message.getHeaders());
        return true;
    }

    @OnTransition(source = "WAIT_RECEIVE",target = "FINISH")
    public boolean receiveTransition(Message message) {
        Order order = (Order)message.getHeaders().get("order");
        assert order != null;
        order.setOrderStatus(OrderStatusEnum.FINISH);
        log.info("完成，状态机反馈信息：{}",message.getHeaders());
        return true;
    }
}
